package org.jpropeller.problem.impl;

import org.jpropeller.bean.impl.BeanDefault;
import org.jpropeller.problem.Problem;
import org.jpropeller.properties.Prop;

/**
 * The IgnorableProblem is a Problem that can be ignored depending on the state of a Prop<Boolean>.
 * The IgnorableProblem is intended for fairly serious problems that should be brought to the user's attention, but which the user may decide to ignore.
 * {@link ProblemCellRenderer}, {@link ProblemCellEditor} and {@link ProblemTableRowView} can be used to show IgnorableProblem in a JTable.
 */
public class IgnorableProblem extends BeanDefault implements Problem {

	private final Prop<String> description;
	private final Prop<String> location;
	private final Prop<ProblemSeverity> severity;
	private final Prop<Boolean> ignore;
	private final Prop<String> ignoreText;
	
	/**
	 * Create a new {@link IgnorableProblem}
	 * 
	 * @param description 	see {@link #description()}
	 * @param location 		see {@link #location()}
	 * @param severity 		see {@link #severity()}
	 * @param ignore 		Ignore this problem
	 */
	public IgnorableProblem(String description, String location,
			ProblemSeverity severity,final Prop<Boolean> ignore) {
		this(description,location,severity,ignore,"Ignore this.");
	}
	
	@Override
	public boolean equals(Object o)
	{
		if(o instanceof IgnorableProblem)
		{
			IgnorableProblem p=(IgnorableProblem)o;
			if((p.description.get()==null && description.get()==null) || (p.description.get().equals(description.get())))
			{
			if((p.location.get()==null && location.get()==null) || (p.location.get().equals(location.get())))
			{
			if((p.severity.get()==null && severity.get()==null) || (p.severity.get().equals(severity.get())))
			{
			if(p.ignore==ignore) // has to be the exact same prop
			{
			if((p.ignoreText.get()==null && ignoreText.get()==null) || (p.ignoreText.get().equals(ignoreText.get())))
			{
				return true;
			}
			}
			}
			}
			}
		}
		return false;
	}

	/**
	 * Create a new {@link IgnorableProblem}
	 * 
	 * @param description 	see {@link #description()}
	 * @param location 		see {@link #location()}
	 * @param severity 		see {@link #severity()}
	 * @param ignore 		Ignore this problem
	 * @param ignoreText	Text associated with the ignore option
	 */
	public IgnorableProblem(String description, String location,
			ProblemSeverity severity,final Prop<Boolean> ignore,
			String ignoreText) {
		super();
		this.severity = create(ProblemSeverity.class, "severity", severity);
		this.description = create("description", description);
		this.location = create("location", location);
		this.ignore = addProp(ignore);
		this.ignoreText= create("ignoreText", ignoreText);
	}
	
	public Prop<Boolean> ignore(){return ignore;}
	public Prop<String> ignoreText(){return ignoreText;}

	@Override
	public Prop<String> description() {return description;}

	@Override
	public Prop<String> location() {return location;}

	@Override
	public Prop<ProblemSeverity> severity() {return severity;}
	
	@Override
	public String toString() {
		return description().get() + " at " + location().get() + ", " + severity().get();
	}

}